package com.exsun.ex1077.ui.activity.bounds

import androidx.databinding.ObservableArrayList
import androidx.databinding.ObservableField
import androidx.lifecycle.MutableLiveData
import com.alibaba.fastjson.JSONArray
import com.alibaba.fastjson.JSONObject
import com.amap.api.maps.model.LatLng
import com.amap.api.maps.model.Marker
import com.exsun.ex1077.app.Constant
import com.exsun.ex1077.network.apiService
import com.exsun.ex1077.network.entity.Bounds
import com.zhangping.amap.extend.getCenterPoint
import com.zhangping.fastjetpack.base.BaseViewModel
import com.zhangping.fastjetpack.extend.base.ResultState
import com.zhangping.fastjetpack.extend.base.request
import com.zhangping.fastjetpack.extend.databind.BooleanObservableField
import com.zhangping.fastjetpack.extend.databind.IntObservableField
import com.zhangping.fastjetpack.model.Tree

/**
 * 描述：
 * 作者：ZhangPing
 * 创建时间：2023/3/27 10:46
 */
class BoundsDrawViewModel: BaseViewModel() {

    //页面类型
    var pageType = IntObservableField(Constant.PAGE_TYPE_BOUNDS_ADD)
    //是否是实地绘制
    var isSiteDraw = BooleanObservableField()
    //围栏对象
    val model = ObservableField<Bounds>()
    //点集合
    val pointList = ObservableArrayList<Marker>()

    val deptTreeResult = MutableLiveData<ResultState<List<Tree>>>()
    val boundsAddResult = MutableLiveData<ResultState<Any?>>()
    val boundsUpdateResult = MutableLiveData<ResultState<Any?>>()

    fun loadDeptTree(){
        request({ apiService.vehicleDeptTree()}, deptTreeResult)
    }

    fun boundsAdd(name: String, companyId: String, pointList: List<LatLng>){
        val centerLatLng = getCenterPoint(pointList)
        request({ apiService.boundsSave(JSONObject().apply {
            put("name", name)
            put("companyId", companyId)
            put("centerLat", centerLatLng.latitude)
            put("centerLng", centerLatLng.longitude)
            put("type", Constant.BOUNDS_TYPE_POLYGON)
            put("mapxyStr", transformToJsonStr(pointList))
        })}, boundsAddResult, isShowDialog = true)
    }

    fun boundsUpdate(name: String, companyId: String, pointList: List<LatLng>){
        val centerLatLng = getCenterPoint(pointList)
        request({ apiService.boundsUpdate(JSONObject().apply {
            put("id", model.get()!!.id)
            put("name", name)
            put("companyId", companyId)
            put("centerLat", centerLatLng.latitude)
            put("centerLng", centerLatLng.longitude)
            put("type", Constant.BOUNDS_TYPE_POLYGON)
            put("mapxyStr", transformToJsonStr(pointList))
        })}, boundsUpdateResult, isShowDialog = true)
    }

    fun boundsDelete(): MutableLiveData<Boolean> {
        val liveData = MutableLiveData<Boolean>()
        request({ apiService.boundsDelete(model.get()!!.id)}, {
            liveData.value = true
        }, isShowDialog = true)
        return liveData
    }

    private fun transformToJsonStr(pointList: List<LatLng>): String{
        if (pointList.isNullOrEmpty()) return ""
        val aoLatLng = JSONArray()
        pointList.forEach {
            aoLatLng.add(JSONObject().apply {
                put("lat", it.latitude)
                put("lng", it.longitude)
            })
        }
        return JSONObject().apply {
            put("aoLatLng", aoLatLng)
        }.toJSONString()
    }

}